﻿using System;
using Serilog;
using Microsoft.Extensions.Configuration;

using Autofac;
using Autofac.Configuration;
using Autofac.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;    // for DbContextOptionsBuilder

namespace CoworkspaceAdmin
{


    class Program
    {
        public static IContainer Container { get; private set; }
        public static string SqlConnectionString { get; set; }

        static public T GetSingletonService<T>()
        {
            var scope = Container.BeginLifetimeScope();
            return scope.Resolve<T>();
        }

        static void test(){
            using (var scope = Container.BeginLifetimeScope()) {
            var ctx = scope.Resolve<Models.postgresContext>();
            Log.Debug("ctx 0: {0}", ctx);
            ctx.User.Add(new Models.User(){
                Company = "成都三医智汇科技有限公司",
                Id = "abc",
                Name = "like"
            });
            ctx.SaveChanges();
            }
        }

        static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Console()
                .CreateLogger();

            
            ConfigureLog();
            ConofigProgramDI();            

            // test();
            var server  = new CoworkspaceAdmin.Server.Server2();
            server.Start();
            // server.test();
            Log.Debug("end of main.");
            // test();

            // System.in.read();
            Console.ReadLine(); 
        }

        static void ConfigureLog()
        {
            Console.WriteLine("Hello World!");
            Log.Information("info");
            Log.Debug("debug");
            Log.Warning("warn");
            Log.Error("error");
        }

        static void ConofigProgramDI()
        {
            var config = new ConfigurationBuilder();
            /*
             * Dependency Injection Options:
             * - "per-dependency"
             * - "single-instance"           
            */
            
            // config.AddJsonFile comes from Microsoft.Extensions.Configuration.Json
            config.AddJsonFile("autofac_configure.json");
            config.AddJsonFile("db_configure.json");

            var configureRoot = config.Build();
            SqlConnectionString = configureRoot.GetConnectionString("DB");
            Log.Debug("DB: {0}", SqlConnectionString);

            // Register the ConfigurationModule with Autofac.
            var module = new ConfigurationModule(configureRoot);
            var containerBuilder = new ContainerBuilder();
            containerBuilder.Register(c =>
                {
                    var optionsBuilder = new DbContextOptionsBuilder<Models.postgresContext>();
                    optionsBuilder.UseNpgsql(SqlConnectionString);
                    // return optionsBuilder.Options;  
                    return new Models.postgresContext(optionsBuilder.Options);
                    /* DbContextOptions<TContext> options
                       means register a component with DbContextOptions<DataModel.CompanyManager>,
                       when needed DbContextOptions<DataModel.CompanyManager>, autofac will use this 
                       expression to do this.
                     */
                }
            ).InstancePerLifetimeScope();
            containerBuilder.RegisterType<Controller.CompanyManager>().As<Controller.ICompanyManager>();
            containerBuilder.RegisterType<Controller.UserManager>().As<Controller.IUserManager>();
            containerBuilder.RegisterType<Controller.ProjectManager>().As<Controller.IProjectManager>();

            Container = containerBuilder.Build();

            // var serviceProvider = new AutofacServiceProvider(Container);
        }
        
    }
}
